package com.wzq.solution;

class Solution33 {
    public static int search(int[] nums, int target) {
        int i=0;
        int j=nums.length-1;
        while(i<=j){

            if(target == nums[i]){
                return i;
            }else if(target == nums[j]){
                return j;
            }else if(target > nums[i]){
                int mid = (i+j)/2;
                if(target == nums[mid]){
                    return mid;
                }else if(target < nums[mid]){
                    j = mid-1;
                }else{
                    if(nums[mid] < nums[i]){
                        j = mid-1;
                    }else{
                        i = mid+1;
                    }
                }
            }else if(target < nums[j]){
                int mid = (i+j)/2;
                if(target == nums[mid]){
                    return mid;
                }else if(target > nums[mid]){
                    i = mid+1;
                }else{
                    if(nums[mid] > nums[j]){
                        i = mid+1;
                    }else{
                        j = mid-1;
                    }
                }
            }else{
                return -1;
            }
        }
        return -1;
        
    }


    public static void main(String[] args) {
        int[] nums = new int[]{8,1,2,3,4,5,6,7};
        System.out.println(search(nums,6));
        System.out.println(search(nums,-9));
        System.out.println(search(nums,0));
        System.out.println(search(nums,3));
        System.out.println(search(nums,4));
        System.out.println(search(nums,5));
        System.out.println(search(nums,6));
        System.out.println(search(nums,7));
        System.out.println(search(nums,1));
        System.out.println(search(nums,2));

    }
}